package com.cs.framework.ui.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.core.io.Resource;

import com.cs.framework.common.util.CacheUtils;
import com.cs.framework.common.util.IOUtils;
import com.cs.framework.common.util.StringUtils;
import com.cs.framework.core.util.CoreConstant;
import com.cs.framework.ui.model.FieldInfo;
import com.cs.framework.ui.model.PageInfo;
import com.cs.framework.ui.model.PanelInfo;
import com.cs.framework.ui.model.UIInfo;

/**
 * UI工具类
 * 
 * @author hedongzhou
 * 
 */
public class UIUtils {

	/**
	 * 判断是否为页面信息
	 * 
	 * @param info
	 * @return
	 */
	public static boolean isPage(UIInfo info) {
		if (info instanceof PageInfo) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否为面板信息
	 * 
	 * @param info
	 * @return
	 */
	public static boolean isPanel(UIInfo info) {
		if (info instanceof PanelInfo) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否为字段信息
	 * 
	 * @param info
	 * @return
	 */
	public static boolean isField(UIInfo info) {
		if (info instanceof FieldInfo) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取页面模板资源路径
	 * 
	 * @param name
	 *            模板名称
	 * @return
	 */
	public static String getPageTemplatePath(String name) {
		return String.format(UIConstant.PAGE_TEMPLATE_PATH, name);
	}

	/**
	 * 导出视图
	 * 
	 * @param name
	 *            模板名称
	 * @param parameter
	 *            参数
	 * @param writer
	 * @throws Exception
	 */
	public static void exportView(String name, Map<String, Object> parameter, Writer writer) throws Exception {
		String templatePath = getPageTemplatePath(name);
		Resource pageTemplate = IOUtils.loadOneResource(templatePath);
		VelocityContext vc = new VelocityContext(parameter);
		Velocity.evaluate(vc, writer, "CS", new InputStreamReader(pageTemplate.getInputStream(), "UTF-8"));
	}

	/**
	 * 获取页面js脚本
	 * 
	 * @param pageId
	 *            页面ID
	 * @param packageUrl
	 *            脚本所在包路径
	 * @return
	 */
	public static String getPageJs(String pageId, String packageUrl) {
		String js = CacheUtils.get("pagejs", pageId);

		if (StringUtils.isBlank(js)) {
			Resource[] resArr = IOUtils.loadResource(CoreConstant.CLASSPATH_PATTERN + packageUrl + "/**/event/"
					+ pageId + "/*.js");

			StringBuffer sb = new StringBuffer();

			sb.append("[\n");

			if (resArr != null && resArr.length > 0) {
				for (int i = 0; i < resArr.length; i++) {
					Resource res = resArr[i];

					BufferedReader br = null;

					try {
						br = new BufferedReader(new InputStreamReader(res.getInputStream(), "UTF-8"));
						sb.append("{query:'" + br.readLine().trim() + "',\n");
						sb.append("event:'" + br.readLine().trim() + "',\n");
						sb.append("fn:");
						String line = null;
						while ((line = br.readLine()) != null) {
							sb.append(line + "\n");
						}
						if (i < resArr.length - 1) {
							sb.append("},\n");
						} else {
							sb.append("}\n");
						}
					} catch (Exception e) {
						throw new RuntimeException(e);
					} finally {
						if (br != null) {
							try {
								br.close();
							} catch (IOException e) {
							}
						}
					}
				}
			}

			sb.append("]");

			js = sb.toString();

			CacheUtils.put("pagejs", pageId, js);
		}

		return js;
	}

}
